package ${basepackage}.app.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
<#list table.additionalSystemImportList as additionalImport>
import ${additionalImport};
</#list>

import ${importbasepackage}.util.BeanUtils;
import ${importbasepackage}.framework.base.controller.BaseSpringController;
import ${basepackage}.app.model.${ClassName};
import ${basepackage}.app.vo.query.${ClassName}Query;
import ${basepackage}.app.service.${ClassName}Service;
<#list table.additionalImportList as additionalImport>
import ${basepackage}.${additionalImport};
</#list>
<#list table.additionalPredefinedImportList as additionalImport>
import ${importbasepackage}.${additionalImport};
</#list>


@Controller
@RequestMapping("/${table.tableJavaName}")
public class ${ClassName}Controller extends BaseSpringController
{
	
	@Autowired
	private ${ClassName}Service ${table.tableJavaName}Service;
	<#list table.additionalAutowired as autowired>
	@Autowired
	private ${autowired?cap_first} ${autowired};
	</#list>
	
	<#list table.importedKeyList as key>
	public List<${key.referencedTableJavaField?cap_first}> get${key.columnNameJavaField?cap_first}List(){
		return ${key.referencedTableJavaField}Service.findAll();
	}	
	</#list>
	
	
	@RequestMapping(value={"","/","/list"})
	public ModelAndView list(${ClassName}Query query, HttpServletRequest request)
	{
		Page<${ClassName}> page = ${table.tableJavaName}Service.findPage(query);
		request.getSession().setAttribute(${ClassName}.class.getSimpleName(), query);
		ModelAndView result = new ModelAndView("/${ClassName}/list");
		ModelMap map = toModelMap(page, query);
		<#list table.columnList as column>
		<#if column.dataDictField>
		map.put("${column.javaField}Map", sysDictService.getDictMapByCode("${column.dictCode}"));
		</#if>
		</#list>
		<#list table.importedKeyList as key>
		map.put("${key.column.javaField}List", get${key.columnNameJavaField?cap_first}List());	
		</#list>
		syncMessage(request,map);
		result.addAllObjects(map);
		return result;		
	}
	
	@RequestMapping("/popup/view")
	public ModelAndView view(${ClassName} ${table.tableJavaName}){
		ModelAndView result = new ModelAndView("/${ClassName}/view");
		ModelMap map = new ModelMap();
		if(${table.tableJavaName}.getId() != null){
			${table.tableJavaName} = ${table.tableJavaName}Service.getById(${table.tableJavaName}.getId());
		}
		if(${table.tableJavaName} == null || ${table.tableJavaName}.getId() == null){
			map.addAttribute("errorKey", "error.record.notfound");
			return new ModelAndView("redirect:/${table.tableJavaName}/popup/create");
		}
		map.addAttribute("${table.tableJavaName}", ${table.tableJavaName});
		<#list table.columnList as column>
		<#if column.dataDictField>
		map.put("${column.javaField}Map", sysDictService.getDictMapByCode("${column.dictCode}"));
		</#if>
		</#list>
		<#list table.importedKeyList as key>
		map.put("${key.column.javaField}List", get${key.columnNameJavaField?cap_first}List());	
		</#list>
		result.addAllObjects(map);
		return result;
	}
	
	@RequestMapping("/popup/create")
	public ModelAndView create(${ClassName} ${table.tableJavaName}){
		ModelAndView result = new ModelAndView("/${ClassName}/add");
		ModelMap map = new ModelMap();
		if(${table.tableJavaName}.getId() != null){
			${table.tableJavaName} = ${table.tableJavaName}Service.getById(${table.tableJavaName}.getId());
		}
		if(${table.tableJavaName} == null){
			map.addAttribute("errorKey", "error.record.notfound");
			${table.tableJavaName} = new ${table.tableJavaName?cap_first}();
		}
		map.addAttribute("${table.tableJavaName}", ${table.tableJavaName});
		<#list table.columnList as column>
		<#if column.dataDictField>
		map.put("${column.javaField}Map", sysDictService.getDictMapByCode("${column.dictCode}"));
		</#if>
		</#list>
		<#list table.importedKeyList as key>
		map.put("${key.column.javaField}List", get${key.columnNameJavaField?cap_first}List());	
		</#list>
		result.addAllObjects(map);
		return result;
	}
	
	@RequestMapping("/popup/save")
	public ModelAndView save(@Valid ${ClassName} ${table.tableJavaName},BindingResult validateResult,HttpServletRequest request){
		if(validateResult.hasErrors()){
			return create(${table.tableJavaName});
		}
		
		<#list table.importedKeyList as key>
		if(${table.tableJavaName}.get${key.columnNameJavaField?cap_first}() != null && ${table.tableJavaName}.get${key.columnNameJavaField?cap_first}().getId() == null){
			${table.tableJavaName}.set${key.columnNameJavaField?cap_first}(null);
		}else{
			${table.tableJavaName}.set${key.columnNameJavaField?cap_first}(${key.referencedTableJavaField}Service.getById(${table.tableJavaName}.get${key.columnNameJavaField?cap_first}().getId()));
		}
		</#list>
		
		if(${table.tableJavaName}.getId() != null){
			${ClassName} existing = ${table.tableJavaName}Service.getById(${table.tableJavaName}.getId());
			BeanUtils.copyProperties(existing, ${table.tableJavaName});
			${table.tableJavaName}Service.save(existing);
			addSuccessMessageInSession(request, false);
		}else{
			${table.tableJavaName}Service.save(${table.tableJavaName});
			addSuccessMessageInSession(request, true);
		}
		
		ModelMap map = new ModelMap();
		ModelAndView result = new ModelAndView("/saveResult");
		map.addAttribute("msg", "success");
		result.addAllObjects(map);
		return result;
	}
}